"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Article_1 = __importDefault(require("../model/Article"));
const sequelize_1 = require("sequelize");
const ArticleComment_1 = __importDefault(require("../model/ArticleComment"));
exports.default = {
    getTopHeat() {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAll({
                order: [["browesNum", "DESC"]],
                limit: 7,
                attributes: ["title", "id"],
            });
            if (result instanceof Object)
                return JSON.parse(JSON.stringify(result));
            return null;
        });
    },
    getTopTime() {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAll({
                order: [["createdTime", "DESC"]],
                limit: 7,
                attributes: ["title", "id"],
            });
            if (result instanceof Object)
                return JSON.parse(JSON.stringify(result));
            return null;
        });
    },
    getTopGift() {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAll({
                order: [["fontlength", "DESC"]],
                limit: 7,
                attributes: ["title", "id"],
            });
            if (result instanceof Object)
                return JSON.parse(JSON.stringify(result));
            return null;
        });
    },
    getAlllimit(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAndCountAll({
                offset: (+data.x - 1) * +data.y,
                limit: +data.y,
                distinct: true,
                include: [
                    {
                        model: ArticleComment_1.default,
                        attributes: { exclude: ["deletedAt"] },
                    },
                ],
            });
            if (result instanceof Object)
                return JSON.parse(JSON.stringify(result));
            return undefined;
        });
    },
    getFindTitle(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAndCountAll({
                offset: (data.x - 1) * data.y,
                limit: data.y,
                attributes: { exclude: ["createdAt", "updatedAt", "deletedAt"] },
                where: {
                    [sequelize_1.Op.or]: {
                        titles: {
                            [sequelize_1.Op.like]: `%${data.title}%`,
                        },
                        title: {
                            [sequelize_1.Op.like]: `%${data.title}%`,
                        },
                        author: {
                            [sequelize_1.Op.like]: `%${data.title}%`,
                        },
                        label: {
                            [sequelize_1.Op.like]: `%${data.title}%`,
                        },
                    },
                },
                distinct: true,
                include: [
                    {
                        model: ArticleComment_1.default,
                        attributes: { exclude: ["deletedAt"] },
                    },
                ],
            });
            return JSON.parse(JSON.stringify(result));
        });
    },
    add(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.create(data);
            return result;
        });
    },
    findpk(id) {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findByPk(id, {
                attributes: { exclude: ["createdAt", "updatedAt", "deletedAt"] },
                include: [
                    {
                        model: ArticleComment_1.default,
                        attributes: { exclude: ["deletedAt"] },
                    },
                ],
            });
            if (!result) {
                return null;
            }
            return result.toJSON();
        });
    },
    update(id, data) {
        return __awaiter(this, void 0, void 0, function* () {
            const [result] = yield Article_1.default.update(data, { where: { id } });
            return result;
        });
    },
    Alllabel() {
        return __awaiter(this, void 0, void 0, function* () {
            const result = yield Article_1.default.findAll({
                attributes: ["label"],
            });
            return result;
        });
    },
    //获取排序数据
    getTimeTop(x, y, z) {
        return __awaiter(this, void 0, void 0, function* () {
            console.log(1);
            const result = yield Article_1.default.findAndCountAll({
                order: [["createdTime", +z > 0 ? "ASC" : "DESC"]],
                offset: (+x - 1) * +y,
                limit: +y,
                distinct: true,
                include: [
                    {
                        model: ArticleComment_1.default,
                        attributes: { exclude: ["deletedAt"] },
                    },
                ],
            });
            return result;
        });
    },
    getBrowesNumTop(x, y, z) {
        return __awaiter(this, void 0, void 0, function* () {
            console.log(2);
            const result = yield Article_1.default.findAndCountAll({
                order: [["browesNum", +z > 0 ? "ASC" : "DESC"]],
                offset: (+x - 1) * +y,
                limit: +y,
                distinct: true,
                include: [
                    {
                        model: ArticleComment_1.default,
                        attributes: { exclude: ["deletedAt"] },
                    },
                ],
            });
            return result;
        });
    },
};
